package com.xiaoc.bulldozer.controller;

import com.xc.framework.common.page.PageDto;
import com.xc.framework.common.utils.BeanUtil;
import com.xiaoc.bulldozer.common.ResponseCode;
import com.xiaoc.bulldozer.common.dto.FinancialProductDto;
import com.xiaoc.bulldozer.common.dto.FinancialProductQueryDto;
import com.xiaoc.bulldozer.common.model.FinancialProduct;
import com.xiaoc.bulldozer.common.service.FinancialProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/financial/product")
public class FinancialProductController extends AdminBaseController {

    public static final Logger logger = LoggerFactory.getLogger(FinancialProductController.class);

    @Resource(name = "financialProductService")
    private FinancialProductService financialProductService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(FinancialProductQueryDto queryDto) {

        if(queryDto.getPage() == null) queryDto.setPage(1);
        
        int count = financialProductService.getFinancialProductCountByCondition(queryDto);
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageDto", new PageDto(queryDto.getPage(), count, PageDto.PAGE_SIZE));

        if (count == 0) {
            params.put("queryDto", queryDto);
            return renderHtml("financialProduct/list.html", params);
        }

        List<FinancialProduct> financialProductList = financialProductService.getFinancialProductListByCondition(queryDto);
        params.put("financialProductList", financialProductList);
        params.put("queryDto", queryDto);
        return renderHtml("financialProduct/list.html", params);
    }

    
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ModelAndView detail(Integer id) {
        FinancialProduct financialProduct = null;
        if(id != null) {
            financialProduct = financialProductService.getFinancialProductById(id);
        }

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("financialProduct", financialProduct);
        return renderHtml("financialProduct/edit.html", params);
    }

    
    @RequestMapping(value = "/createOrUpdate", method = RequestMethod.POST)
    public ModelAndView createOrUpdate(@RequestBody FinancialProductDto FinancialProductDto) {

        long currentTime = System.currentTimeMillis();
        FinancialProduct financialProduct = BeanUtil.copyProperties(FinancialProduct.class, FinancialProductDto);
        financialProduct.setUpdateTime(currentTime);

        try {

            if (financialProduct.getId() == null) {
                financialProduct.setCreateTime(currentTime);
                financialProductService.save(financialProduct);
            } else {
                financialProductService.update(financialProduct);
            }
        } catch (Exception e) {
            logger.error("createOrUpdate failed", e);
            return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "操作失败");
        }
        return jsonSucess();
    }
    
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ModelAndView delete(@RequestBody FinancialProductDto financialProductDto) {
        if (financialProductDto.getId() == null) {
            return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "该金融产品不存在");
        }
        financialProductService.deleteById(financialProductDto.getId());
        return jsonSucess();
    }

}
